package com.sdjz.club.club.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sdjz.club.club.entity.Message;
import com.sdjz.club.club.mapper.MessageMapper;
import com.sdjz.club.club.service.impl.MessageServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Dftre
 * @since 2022-07-29
 */
@RestController
@RequestMapping("/club/message")
public class MessageController {
    @Autowired
    MessageServiceImp messageService;
    @Autowired
    MessageMapper messageMapper;

    //查询
    @GetMapping("/select")
    public CommonResult select(
            @RequestParam(name = "msg", required = false, defaultValue = "null") String msg,
            @RequestParam(name = "userId", required = false, defaultValue = "0") Long user_id
    ) {
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        if (!msg.equals("null")) wrapper.like("msg", msg);
        if (!user_id.equals(0L)) wrapper.eq("user_id", user_id);
        return new CommonResult(messageMapper.selectList(wrapper));
    }

    // 实现分页查询
    @GetMapping("list")
    public CommonResult selectPage(
            @RequestParam(name = "current") int current,
            @RequestParam(name = "size", required = false, defaultValue = "20") int size
    ) {
        Page<Message> page = new Page<>(current, size);
        messageMapper.selectPage(page, null);
        return new CommonResult(page.getRecords());
    }

    // 实现删除
    @PostMapping("/delete")
    public CommonResult deleteMenu(@RequestParam("id") String id) {
        return CommonResult.succ(messageMapper.deleteById(id));
    }

    @RequestMapping("/deleteByUserId")
    public CommonResult deleteByUserId(@RequestParam("userId") String id) {
        System.out.println(id);
        return CommonResult.succ(messageMapper.delete(
                new QueryWrapper<Message>().eq("user_id", id)
        ));
    }

    // 实现插入
    @PostMapping("/insert")
    public CommonResult addMenu(@RequestBody Message message) {
        return CommonResult.succ(messageMapper.insert(message));
    }

    // 实现更新
    @PostMapping("/update")
    public CommonResult updateMenu(@RequestBody Message message) {
        return CommonResult.succ(messageMapper.updateById(message));
    }

}

